<include file="public@header_vue"/>
<style>
    .el-pagination {
        margin-top: 20px;
    }

    .avatar {
        width: 40px;
        height: auto;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
</style>
</head>
<body>
<div id="app">
    <el-tabs type="border-card">
        <el-tab-pane label="客户管理" v-loading="onDataLoading">
            <el-row>
                <el-col>
                    <el-form :inline="true" :model="form" class="demo-form-inline" size="mini">
                        <el-form-item label="关键词：">
                            <el-input v-model="form.keyword" placeholder="姓名/手机号码/登录账号"></el-input>
                        </el-form-item>
                        <el-form-item label="日期:">
                            <el-date-picker v-model="form.duration" type="daterange" value-format="yyyy-MM-dd"
                                            :picker-options="durationOptions" range-separator="至"
                                            start-placeholder="开始日期" end-placeholder="结束日期" clearable></el-date-picker>
                        </el-form-item>
                        <el-form-item v-if="{$role_id} != 3" label="公司：">
                            <el-select v-model="form.company_id" placeholder="选择公司" @change="change_company_id">
                                <el-option
                                        v-for="item in companyData"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="客户经理：">
                            <el-select v-model="form.manager_id" placeholder="选择客户经理">
                                <el-option
                                        v-for="item in managerList"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="客户状态：">
                            <el-select v-model="form.customer_status_id" placeholder="选择客户状态">
                                <el-option
                                        v-for="item in customerStatus"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="getData()">查询</el-button>
                            <el-button type="danger" @click="clearForm()">清空</el-button>
                            <el-button type="primary" @click="exportData()">导出</el-button>
                            <!--                            <el-button type="primary" size="mini" @click="update()">新增店长</el-button>-->
                        </el-form-item>
                    </el-form>

                </el-col>
            </el-row>
            <el-table :data="tableData" style="width: 100%">
                <el-table-column prop="id" label="ID" width="80"></el-table-column>
                <el-table-column v-if="{$role_id} != 3" prop="company_info.name" label="公司名称"
                                 width="150"></el-table-column>
                <el-table-column prop="store_info.name" label="店铺名称"
                                 width="150"></el-table-column>
                <el-table-column prop="mobile" label="手机号码" width="150"></el-table-column>
                <el-table-column prop="true_name" label="真实姓名" width="100"></el-table-column>
                <el-table-column prop="" label="上级" width="100">
                    <template slot-scope="scope">
                        {{scope.row.recommend_user_info ? scope.row.recommend_user_info.true_name : '公海客户'}}
                    </template>
                </el-table-column>
                <el-table-column prop="" label="角色" width="100">
                    <template slot-scope="scope">
                        {{scope.row.relation_ship ? scope.row.relation_ship.role_name:'客户'}}
                    </template>
                </el-table-column>
                <el-table-column prop="customer_status_name" label="客户状态" width="100"></el-table-column>
                <el-table-column prop="hobby" label="爱好" width="100"></el-table-column>
                <el-table-column prop="family" label="家庭情况" width="100"></el-table-column>
                <el-table-column prop="address" label="联系地址" width="200">
                    <template slot-scope="scope">
                        {{scope.row.province+ scope.row.city+scope.row.area+scope.row.address}}
                    </template>
                </el-table-column>
                <el-table-column prop="source" label="来源" width="150"></el-table-column>
                <el-table-column prop="extend_field" label="扩展信息" width="200"></el-table-column>
                <el-table-column prop="status" label="审核状态" width="100">
                </el-table-column>
                <el-table-column prop="is_finished" label="完成状态" width="100">
                </el-table-column>
                <el-table-column prop="create_time" label="添加时间" width="200">
                    <template slot-scope="scope">
                        {{formatDate(scope.row.create_time)}}
                    </template>
                </el-table-column>
                <el-table-column prop="last_check_time" label="最后跟进时间" width="200">
                    <template slot-scope="scope">
                        {{formatDate(scope.row.last_check_time)}}
                    </template>
                </el-table-column>
                <el-table-column prop="delete_time" label="删除时间" width="200">
                    <template slot-scope="scope">
                        {{formatDate(scope.row.delete_time)}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="180">
                    <template slot-scope="scope">
<!--                        <el-button size="mini" type="text" @click="update(scope.row)">编辑</el-button>-->
                        <el-button size="mini" type="text" @click="delete_customer(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination background layout="total,prev, pager, next" :total="totalNum" :page-size="10"
                           :current-page.sync="currentPage" @current-change="getData"></el-pagination>
        </el-tab-pane>
        <el-dialog :title="userForm.id == '' ? '新增店长' : '修改店长'" :visible.sync="dialogVisible" width="60%"
                   v-loading.fullscreen="dialogLoading">
            <el-form :model="userForm" class="demo-form-inline" :rules="rules" ref="userForm"
                     label-width="200px">
                <!--                <el-form-item label="选择角色" prop="name">-->
                <!--                    <el-col :span="18">-->
                <!--                        <el-select v-model="userFrom.role_id" placeholder="请选择角色">-->
                <!--                            <el-option v-for="role in role_list" :label="role.name" :value="role.id"></el-option>-->
                <!--                        </el-select>-->
                <!--                    </el-col>-->
                <!--                </el-form-item>-->
                <el-form-item label="店长姓名" prop="true_name">
                    <el-col :span="18">
                        <el-input v-model="userForm.true_name" placeholder="姓名"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item v-if="{$role_id} != 3" label="公司" prop="company_id">
                    <el-select v-model="userForm.company_id" placeholder="选择公司">
                        <el-option
                                v-for="item in companyData"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="手机号码" prop="user_login">
                    <el-col :span="18">
                        <el-input v-model="userForm.user_login" placeholder="手机号码登陆"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="登陆密码" prop="user_pass">
                    <el-col :span="18">
                        <el-input type="password" v-model="userForm.user_pass" placeholder="登陆密码"></el-input>
                    </el-col>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
            <el-button v-if="userForm.id === ''" type="primary" @click="submit('userForm')">新增</el-button>
            <el-button v-else type="primary" @click="submit('userForm')">修改</el-button>
        </span>
        </el-dialog>
    </el-tabs>
</div>
</body>
<script>
    let id = 1000;
    var app = new Vue({
        el: '#app',
        data: function () {

            var checkPhone = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('手机号不能为空'));
                } else {
                    const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
                    if (reg.test(value)) {
                        callback();
                    } else {
                        return callback(new Error('请输入正确的手机号'));
                    }
                }
            };
            let self = this;
            return {
                tableData: [],
                companyData: [],
                managerList: [],
                customerStatus: [],
                form: {
                    keyword: '',
                    company_id: '',
                    duration: '',
                    manager_id: '',
                    customer_status_id: '',
                },
                userForm: {
                    id: '',
                    true_name: '',
                    user_login: '',
                    user_pass: '',
                },
                dialogVisible: false,
                dialogLoading: true,
                roleData: [],
                totalNum: 0,
                currentPage: 1,
                parent_info: [],
                show_name: {
                    label: 'name'
                },
                durationOptions: {
                    shortcuts: [{
                        text: '一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                onDataLoading: false,
                headers: {'X-Requested-With': 'XMLHttpRequest'},
                rules: {
                    true_name: [{required: true, message: '请输入店长姓名', trigger: 'blur'}],
                    user_login: [{required: true, message: '请输入手机号码', trigger: 'blur'}],
                    user_pass: [{required: true, message: '请输入密码', trigger: 'blur'}],
                    company_id: [{required: true, message: '请选择公司', trigger: 'blur'}],
                },
                role_list: [],
            };

        },
        created() {
            this.getData()
            this.getCompanyData()
            if ({$role_id} == 3) {
                this.form.company_id = {$admin_info.company_id};
            }

            // this.get_customer_status()
        },
        methods: {
            clearForm() {
                this.form = {
                    keyword: '',
                    company_id: '',
                    duration: '',
                    manager_id: '',
                    customer_status_id: ''
                }
            },
            exportData() {
                let _this = this;
                let params = {};
                this.onDataLoading = true;
                if (this.form.keyword !== '') {
                    params.keyword = this.form.keyword;
                }

                if (this.form.duration) {
                    params.duration = this.form.duration;
                }
                if (this.form.company_id != '') {
                    params.company_id = this.form.company_id;
                }
                axios.get("{:url('admin/role/export')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                    params: params
                }).then(function (response) {
                    console.log(response);
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        window.location.href = response.data.url
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                    console.log(_this.tableData)
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            change_company_id() {
                let _this = this;
                let company_id = _this.form.company_id; //get_manager_list
                this.get_customer_status();
                axios.get("{:url('admin/role/get_manager_list')}?company_id=" + company_id, {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                }).then(function (response) {
                    _this.onDataLoading = false;
                    _this.form.manager_id = '';
                    if (response.data.code === 1) {
                        _this.managerList = response.data.data;
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            getCompanyData() {
                let _this = this;
                axios.get("{:url('admin/company/get_company_list')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                }).then(function (response) {
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {

                        if (response.data.data.length == 0) {
                            _this.get_customer_status();
                            _this.change_company_id();
                        } else {
                            _this.companyData = response.data.data;
                        }

                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            get_customer_status() {
                let _this = this;
                this.onDataLoading = true;
                axios.get("{:url('admin/customer_status/index')}?company_id=" + this.form.company_id, {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    }
                }).then(function (response) {
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.customerStatus = response.data.data;
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            assign_permissions(node, data) {
                console.log(data)
            },
            update(data) {
                let _this = this;
                console.log(data)
                if (data) {
                    _this.userForm = data
                } else {
                    _this.userForm = {
                        id: '',
                        true_name: '',
                        user_login: '',
                        user_pass: '',
                        company_id: '',
                    }
                }
                _this.dialogVisible = true;
                _this.dialogLoading = false;
            },
            append(data) {
                let _this = this;
                console.log(data)
                _this.dialogVisible = true;
                _this.roleFrom.parent_id = data.parent_id;
                _this.dialogLoading = false;
            },
            submit(form) {
                let _this = this;
                _this.$refs[form].validate((valid) => {
                    if (valid) {
                        axios.post("{:url('admin/role/update_user')}", this.userForm, {
                            headers: {
                                'X-Requested-With': 'XMLHttpRequest',
                            }
                        }).then(function (response) {
                            _this.dialogLoading = false;
                            if (response.data.code === 1) {
                                if (_this.userForm.id === '') {
                                    _this.$message.success('新增完成');
                                } else {
                                    _this.$message.success('修改成功');
                                }
                                _this.getData();
                                _this.dialogVisible = false;
                            } else {
                                _this.$message.warning(response.data.msg);
                            }
                        }).catch(function (error) {
                            _this.dialogLoading = false;
                            _this.$message.error(error.toString())
                        })
                    } else {
                        _this.dialogLoading = false;
                        return false;
                    }
                });
            },
            remove(node, data) {
                let _this = this;
                const parent = node.parent;
                const children = parent.data.children || parent.data;
                const index = children.findIndex(d => d.id === data.id);
                console.log(children[index].children)
                if (children[index].children == undefined || children[index].children.length == 0) {
                    children.splice(index, 1);
                } else {
                    _this.$message.error('该角色下还有未删除的角色');
                }
            },
            delete_customer(id) {
                let _this = this;
                let params = {id: id};
                axios.post("{:url('admin/role/delete_user')}", params, {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    }
                }).then(function (response) {
                    _this.dialogLoading = false;
                    if (response.data.code === 1) {
                        _this.$message.success('删除完成');
                        _this.getData(_this.currentPage);
                    } else {
                        _this.$message.warning(response.data.msg);
                    }
                }).catch(function (error) {
                    _this.dialogLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            //首页获取列表数据
            getData(page = 1) {
                let _this = this;
                let params = {page: page};
                this.onDataLoading = true;
                if (this.form.keyword !== '') {
                    params.keyword = this.form.keyword;
                }
                if (this.form.duration) {
                    params.duration = this.form.duration;
                }
                if (this.form.company_id != '') {
                    params.company_id = this.form.company_id;
                }
                if (this.form.manager_id) {
                    params.manager_id = this.form.manager_id;
                }
                if (this.form.customer_status_id) {
                    params.customer_status_id = this.form.customer_status_id;
                }
                axios.get("{:url('admin/role/user_list')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                    params: params
                }).then(function (response) {
                    console.log(response);
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.tableData = response.data.data.data;
                        _this.totalNum = response.data.data.total;
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                    console.log(_this.tableData)
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },

            formatDate(timestamp) {
                if (timestamp) {
                    let date = new Date(timestamp * 1000);
                    return date.getFullYear() + '-' + this.prefix(date.getMonth() + 1) + '-' + this.prefix(date.getDate()) + ' ' + this.prefix(date.getHours()) + ':' + this.prefix(date.getMinutes()) + ':' + this.prefix(date.getSeconds());
                } else {
                    return '';
                }
            },
            prefix(num) {
                if (num < 10) {
                    return '0' + num;
                }
                return num;
            }
        }
    });
</script>
